<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="领用单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiveCode']" placeholder="请输入领用单号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="领用类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiveType']" placeholder="请输入领用类型"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="领用部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiveDepart']" placeholder="请输入领用部门"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="领用人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receivePerson']" placeholder="请输入领用人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="领用备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['receiveNote']" rows="4" placeholder="请输入领用备注"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="备品领用-明细信息" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesBackupReceiveitemTable.loading"
            :columns="mesBackupReceiveitemTable.columns"
            :dataSource="mesBackupReceiveitemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesBackupReceiveinfoModal',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['mesBackupReceiveitem', ],
        tableKeys:['mesBackupReceiveitem', ],
        activeKey: 'mesBackupReceiveitem',
        // 备品领用-明细信息
        mesBackupReceiveitemTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料SN',
              key: 'materielSn',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料料号',
              key: 'materielCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料数量',
              key: 'materielNum',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料名称',
              key: 'materielName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料规格',
              key: 'materielGague',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '库存数量',
              key: 'storageNum',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '领用数量',
              key: 'receiveNum',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '生产日期',
              key: 'produceDate',
              type: FormTypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '入库日期',
              key: 'enterwareDate',
              type: FormTypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '入库人',
              key: 'enterwarePerson',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/produce/mesBackupReceiveinfo/add",
          edit: "/produce/mesBackupReceiveinfo/edit",
          mesBackupReceiveitem: {
            list: '/produce/mesBackupReceiveinfo/queryMesBackupReceiveitemByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'receiveCode','receiveType','receiveDepart','receivePerson','receiveNote')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesBackupReceiveitem.list, params, this.mesBackupReceiveitemTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          mesBackupReceiveitemList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'receiveCode','receiveType','receiveDepart','receivePerson','receiveNote'))
     },

    }
  }
</script>

<style scoped>
</style>